package com.visual.android;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    TextView logTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        logTextView = (TextView) findViewById(R.id.tv_log_output);
        logTextView.setText("下面为调试输出：");
        logTextView.append(StrUtils.formatMsg("1.getFilesDir()", getFilesDir().getAbsolutePath()));

        logTextView.append(StrUtils.formatMsg("2.getCacheDir()", getCacheDir().getAbsolutePath()));

        logTextView.append(StrUtils.formatMsg("3.getFileOutputPath()", getFileOutputPath()));

        logTextView.append(StrUtils.formatMsg("4.getTempFilePath()", getTempFilePath()));

        logTextView.append(StrUtils.formatMsg("5.getPublicAlbumStorageDir()", getPublicAlbumStorageDir()));

        logTextView.append(StrUtils.formatMsg("7.getPrivateAlbumStorageDir()", getPrivateAlbumStorageDir()));

        logTextView.append(StrUtils.formatMsg("8.getFreeSpace()(files下，单位MB)", getFilesDir().getFreeSpace()/1024/1024));

        logTextView.append(StrUtils.formatMsg("9.getTotalSpace()(files下，单位MB)", getFilesDir().getTotalSpace()/1024/1024));
    }

    private String getFileOutputPath() {
        String fileName = "test";
        String string = "Hello world!";
        FileOutputStream outputStream;

        try {
            outputStream = openFileOutput(fileName, this.MODE_PRIVATE);
            outputStream.write(string.getBytes());
            outputStream.close();
            return getFileStreamPath(fileName).getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "null";
    }

    private String getTempFilePath() {
        String fileName = "test";
        try {
            return File.createTempFile(fileName, null, this.getCacheDir()).getAbsolutePath();
        } catch (IOException e) {
            // Error while creating file
        }
        return "null";
    }

    public String getPublicAlbumStorageDir() {
        // Get the directory for the user's public pictures directory.
        String albumName = "album";
        File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), albumName);
        if (!file.mkdirs()) {
            return "Directory not created";
        }
        return file.getAbsolutePath();
    }

    public String getPrivateAlbumStorageDir() {
        // Get the directory for the app's private pictures directory.
        String albumName = "album";
        File file = new File(this.getExternalFilesDir(
                Environment.DIRECTORY_PICTURES), albumName);
        if (!file.mkdirs()) {
            return "Directory not created";
        }
        return file.getAbsolutePath();
    }

    /* Checks if external storage is available for read and write */
    private boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    private boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }
}
